package borge.web.teamspirit.client.page.shared.widgets;

import java.util.List;

import borge.web.teamspirit.client.data.Comment;
import borge.web.teamspirit.client.interfaces.PersistentData;
import borge.web.teamspirit.client.interfaces.ServerCallbackHandler;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler;
import borge.web.teamspirit.client.interfaces.PersistentData.DataKey;
import borge.web.teamspirit.client.page.shared.textinput.WriteCommentDialog;
import borge.web.teamspirit.client.servercomm.DBQuery;
import borge.web.teamspirit.client.servercomm.DBQueryResults;
import borge.web.teamspirit.client.servercomm.ServerComm;
import borge.web.teamspirit.client.servercomm.DBQueryResults.Result;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ShowCommentsWidget extends ShowDataWidget {

	private List<PersistentData> iComments;
	private String iParentKey;
	private FlexTable iTable;
	private VerticalPanel iPanel;
	
	public ShowCommentsWidget(WidgetEventHandler aWeh) {
		super(aWeh);
		
		iTable = new FlexTable();
		iPanel = new VerticalPanel();
		iPanel.add(iTable);
		//iPanel.add(new WriteCommentDialog());
		Button write = new Button();
		write.setText("Write");
		write.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				new WriteCommentDialog();
			}
			
		});
		iPanel.add(write);
		
		initWidget(iPanel);
	}
	
	public void setAndDisplay(List<PersistentData> aComments){
		iComments = aComments;
		createDataTable();
	}
	
	private void createDataTable(){
		
		if(iComments == null)
			return;
		
		int row = 0, column = 0;
    	
    	for(PersistentData pd : iComments){
    		
    		Comment c = (Comment)pd;
    		
    		iTable.setText(row, column++, "Author");
    		iTable.setText(row++, column--, c.getPosterName());
    		iTable.setText(row, column++, "Date");
    		iTable.setText(row++, column--, c.getDate().toString());
    		iTable.setText(row, column++, "Text");
    		iTable.setText(row++, column--, c.getText());
    		
    		
    	}
	}

	@Override
	public boolean needsUpdate(String aData) {

		if(iParentKey == null || !iParentKey.equals(aData)){
			iParentKey = aData;
			return true;
		}
		
		return false;
	}

	@Override
	public void populateData(DBQueryResults results) {

		Result result = results.getResult(DataKey.COMMENT);
		if(result != null){
			iComments = result.getResultList();
		}
		
	}

	@Override
	public void requestServerData(ServerCallbackHandler aHandler) {
		DBQuery query = new DBQuery(DataKey.COMMENT, Comment.class.getName());
		query.addFilter("iParentKey", DBQuery.EQUALS, "matchKey", "String", iParentKey, null);
		ServerComm.getObjectList(query, aHandler);
		
	}

}
